package com.xhd.base.scheduler;

import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.xBaseObj;
import com.xhd.config.ActionControl;
import com.xhd.db.XDao;
import com.xhd.server.ProcessHandle;
import com.xhd.server.ProcessHandleMgr;
import com.xhd.server.ServerConfig;
import com.xhd.servlet.ClientAction;
import com.xhd.util.XThread;


public class SchedulerProcessThread extends XThread {
	private ActionControl jm407 = null;
	  private static Vector jm408 = new Vector();
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp13_10 = jm10;
	    if (tmp13_10 == null)
	    {
	      
	      try
	      {
	          SchedulerProcessThread.jm10 = Class.forName("com.sinocc.base.scheduler.SchedulerProcessThread");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp13_10);
	  }

	  public SchedulerProcessThread()
	  {
	    super("SchedulerThread-" + jm408.size());
	    jm408.add(this);
	    this.jm407 = ActionControl.getInstance();
	  }

	  public void run()
	  {
	    int i = 1;
	    SchedulerQueue localSchedulerQueue = SchedulerQueue.getInstance();
	    ClientAction localClientAction = new ClientAction(2);
	    localClientAction.setServiceName("CPCSchProcessThread");
	    localClientAction.setClientIP("127.0.0.1");
	    while (!(this.stop))
	    {
	      if (this.stop)
	        break;
	      if (i != 0)
	      {
	        try
	        {
	          Thread.sleep(getInterval() * 1000);
	        }
	        catch (InterruptedException localInterruptedException)
	        {
	        }
	        if (this.stop)
	          break;
	      }
	      SchedulerTask localSchedulerTask = localSchedulerQueue.popTask();
	      i = (localSchedulerTask == null) ? 1 : 0;
	      if (localSchedulerTask == null)
	        continue;
	      xBaseObj localxBaseObj = localSchedulerTask.getBizObj();
	      String str;
	      if (localxBaseObj != null)
	        str = localxBaseObj.getVaultInfo().getVaultId();
	      else
	        str = ServerConfig.DefaultVault;
	      XDao localCPCDao = new XDao(str);
	      localSchedulerTask.setDao(localCPCDao);
	      try
	      {
	        if (localSchedulerTask.startProcess())
	        {
	          localCPCDao.commitContinue();
	          localClientAction.setMethodName(localSchedulerTask.getClientAction().getFullName());
	          ProcessHandle localProcessHandle = ProcessHandleMgr.openHandle(localClientAction, localxBaseObj);
	          Throwable localThrowable2 = this.jm407.processTransaction(localxBaseObj, localSchedulerTask.getClientAction(), localProcessHandle);
	          localSchedulerTask.setProcessResult(localThrowable2);
	        }
	      }
	      catch (Throwable localThrowable1)
	      {
	        jm9.error(localThrowable1);
	        localSchedulerTask.setProcessResult(localThrowable1);
	      }
	      finally
	      {
	        try
	        {
	          localCPCDao.commit();
	        }
	        catch (Exception localException)
	        {
	          jm9.error(localException);
	        }
	        localCPCDao.freeConnection();
	        ProcessHandleMgr.closeHandle();
	      }
	    }
	    jm9.info("Stop Thread " + getName());
	    jm408.remove(this);
	  }

	  public static void wakeUpAll()
	  {
	    if (jm408 == null)
	      return;
	    for (int i = 0; i < jm408.size(); ++i)
	      ((SchedulerProcessThread)jm408.get(i)).wakeUp();
	  }

	  public static boolean hasThread()
	  {
	    return (jm408.size() > 0);
	  }
}
